(function () {
    const sqlite3 = require('sqlite3');
    const path = require('path');
           
    function Database() {
        this.dbFile = './data/info.db';//path.join(__dirname, '/data/info.db');
        console.log(this.dbFile);
        this.db = new sqlite3.Database(this.dbFile);
    }

    Database.constructor = Database;

    Database.prototype.add = function (table, data, callback) {
        let field = [], val = [], where = [];
        for (let index in data) {
            field.push('"' + index + '"');
            val.push('"' + data[index] + '"');
        }

        let sql = 'insert into ' + table + '( ' + field.join(',') +') values(' + val.join(',') + ')';
        console.log(sql);
        this.db.run(sql, function(err, res) {
            if (typeof callback === 'function') {
                callback(err, res)
            }
        })
    };

    Database.prototype.replace = function (table, data, callback) {
        let field = [], val = [], where = [];
        for (let index in data) {
            field.push(index);
            val.push('\'' + data[index] + '\'');
        }

        let sql = 'replace  into ' + table + '( ' + field.join(',') +') values(' + val.join(',') + ')';
        console.log(sql);
        this.db.run(sql, function(err, res) {
            if (typeof callback === 'function') {
                callback(err, res)
            }
        })
    };


    Database.prototype.update = function (table, conditions, data, callback) {
        let field = [], val = [], where = [];
        for (let index in conditions) {
            if (typeof conditions[index] === 'object')  {
                where.push(index + ' in ( ' + conditions[index].join(',') + ' ) ');
            } else {
                where.push(index + ' = "' + conditions[index] + '"');
            }
        }

        if (where.length < 1) {
            if (typeof callback === 'function') {
                callback('where is null', null)
            }
            return false;
        }

        for (let index in data) {
            field.push('"' + index + '"');
            val.push(index + '="' + data[index] + '"');
        }

        let sql = 'update ' + table + ' set ' + val.join(',') + ' where ' + where.join(' and ');
        console.log(sql);
        this.db.run(sql, function(err, res) {
            if (typeof callback === 'function') {
                callback(err, res)
            }
        })
    };

    Database.prototype.delete = function (table, conditions, callback) {
        let field = [], val = [], where = [];
        for (let index in conditions) {
            if (typeof conditions[index] === 'object')  {
                where.push(index + ' in ( ' + conditions[index].join(',') + ' ) ');
            } else {
                where.push(index + ' = "' + conditions[index] + '"');
            }
        }

        if (where.length < 1) {
            if (typeof callback === 'function') {
                callback('where is null', null)
            }
            return false;
        }

        let sql = 'delete from ' + table +  ' where ' + where.join(' and ');
        console.log(sql);
        this.db.run(sql, function(err, res) {
            if (typeof callback === 'function') {
                callback(err, res)
            }
        })
    };

    Database.prototype.getRow = function(table, conditions, callback) {
        let where = [];
        for (let index in conditions) {
            if (typeof conditions[index] === 'object')  {
                where.push(index + ' in ( ' + conditions[index].join(',') + ' ) ');
            } else {
                where.push(index + ' = "' + conditions[index] + '"');
            }
        }

        let sql = 'select * from ' + table ;
        where.length > 0 && (sql += ' where ' + where.join(' and '));
        sql += ' limit 1';
        console.log(sql);
        this.db.all(sql, function(err, res) {
            if (typeof callback === 'function') {
                callback((res && res.length) ? res[0] : null, err)
            }
        });
    };

    Database.prototype.getAll = function(table, conditions, order, offset, limit, callback) {
        let where = [];
        for (let index in conditions) {
            if (typeof conditions[index] === 'object')  {
                where.push(index + ' in ( ' + conditions[index].join(',') + ' ) ');
            } else {
                where.push(index + ' = "' + conditions[index] + '"');
            }
        }

        let sql = 'select * from ' + table ;
        where.length > 0 && (sql += ' where ' + where.join(' and '));
        order && (sql += ' order by ' + order);
        sql += ' limit ' +  offset + ',' + limit;
        console.log(sql);
        this.db.all(sql, function(err, res) { 
            console.log(res);                            
            if (typeof callback === 'function') {
                callback(res, err)
            }                     
        });
    };

    Database.prototype.getCount = function(table, conditions, callback) {
        let where = [];
        for (let index in conditions) {
            if (typeof conditions[index] === 'object')  {
                where.push(index + ' in(' + conditions[index].join(',') + ')');
            } else {
                where.push(index + ' = "' + conditions[index] + '"');
            }
        }

        let sql = 'select count(*) as ct from ' + table ;
        where.length > 0 && (sql += ' where ' + where.join(' and '));
        sql += ' limit 1';
        console.log(sql);
        this.db.get(sql, function(err, res) {
            if (typeof callback === 'function') {
                if (!err){
                    res = res.ct;
                } else {
                    res = 0;
                }
                callback(res, err)
            }
        });
    };



    window.database = new Database();
})();




